{{>licenseInfo}}
#include "{{classname}}.h"

#include "{{unrealModuleName}}Module.h"
#include "{{modelNamePrefix}}Helpers.h"

#include "Templates/SharedPointer.h"

{{#cppNamespaceDeclarations}}
namespace {{this}} 
{
{{/cppNamespaceDeclarations}}
{{#models}}{{#model}}
{{#hasEnums}}
{{#vars}}
{{#isEnum}}
inline FString ToString(const {{classname}}::{{{enumName}}}& Value)
{
	{{#allowableValues}}
	switch (Value)
	{
	{{#enumVars}}
	case {{classname}}::{{{enumName}}}::{{name}}:
		return TEXT({{{value}}});
	{{/enumVars}}
	}
	{{/allowableValues}}

	UE_LOG(Log{{unrealModuleName}}, Error, TEXT("Invalid {{classname}}::{{{enumName}}} Value (%d)"), (int)Value);	
	return TEXT("");
}

inline FStringFormatArg ToStringFormatArg(const {{classname}}::{{{enumName}}}& Value)
{
	return FStringFormatArg(ToString(Value));
}

inline void WriteJsonValue(JsonWriter& Writer, const {{classname}}::{{{enumName}}}& Value)
{
	WriteJsonValue(Writer, ToString(Value));
}

inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, {{classname}}::{{{enumName}}}& Value)
{
	FString TmpValue;
	if (JsonValue->TryGetString(TmpValue))
	{
		static TMap<FString, {{classname}}::{{{enumName}}}> StringToEnum = { {{#enumVars}}
			{ TEXT({{{value}}}), {{classname}}::{{{enumName}}}::{{name}} },{{/enumVars}} };

		const auto Found = StringToEnum.Find(TmpValue);
		if(Found)
		{
			Value = *Found;
			return true;
		}
	}
	return false;
}

{{/isEnum}}
{{/vars}}
{{/hasEnums}}
void {{classname}}::WriteJson(JsonWriter& Writer) const
{
	{{#parent}}
	#error inheritance not handled right now
	{{/parent}}
	Writer->WriteObjectStart();
    {{#vars}}
	{{#required}}
	Writer->WriteIdentifierPrefix(TEXT("{{baseName}}")); WriteJsonValue(Writer, {{name}});
	{{/required}}
	{{^required}}
	if ({{name}}.IsSet())
	{
		Writer->WriteIdentifierPrefix(TEXT("{{baseName}}")); WriteJsonValue(Writer, {{name}}.GetValue());	
	}
	{{/required}}
    {{/vars}}
	Writer->WriteObjectEnd();
}

bool {{classname}}::FromJson(const TSharedPtr<FJsonObject>& JsonObject)
{
	bool ParseSuccess = true;

    {{#vars}}
	ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("{{baseName}}"), {{name}});
    {{/vars}}

	return ParseSuccess;
}
{{/model}}
{{/models}}
{{#cppNamespaceDeclarations}}
}
{{/cppNamespaceDeclarations}}
